<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['customer:customerInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- 新增序号列 -->
      <el-table-column label="序号" align="center" width="50">
        <template #default="scope">
          <!-- 索引从0开始，+1后作为序号 -->
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <!-- 原有列 -->
      <el-table-column label="昵称" align="center" prop="name" />
      <el-table-column label="手机号" align="center" prop="mobile" />

      <el-table-column label="是否合同签约人" align="center" prop="isContractSigner" >
        <template #default="scope">
          <span v-if="!scope.row.isContractSigner || scope.row.isContractSigner === 0">否</span>
          <span v-else>是</span>
        </template>
      </el-table-column>
      <el-table-column label="服务下单次数" align="center" prop="serviceOrderCount" >
        <template #default="scope">
          <span>{{ scope.row.serviceOrderCount ? scope.row.serviceOrderCount: 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="绑定的老人姓名" align="center" prop="createdAt" width="180">
        <template #default="scope">

          <template v-if="scope.row.elders && scope.row.elders.length > 0">
      <span v-for="(item, index) in scope.row.elders" :key="index">
        <span v-if="index > 0">,</span> {{ item.name }}
      </span>
          </template>
          <!-- 无老人时，显示空或提示文本 -->
          <template v-else>
            <span>无</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="首次登录时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="customerInfoRef" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="CustomerInfo">
import { listCustomerInfo, getCustomerInfo, delCustomerInfo, addCustomerInfo, updateCustomerInfo } from "@/api/customer/customerInfo"

const { proxy } = getCurrentInstance()

const customerInfoList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    mobile: null,
    name: null,
    avatar: null,
    createdAt: null,
    serviceOrderCount: null,
    isContractSigner: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询客户信息列表 */
function getList() {
  loading.value = true
  listCustomerInfo(queryParams.value).then(response => {
    customerInfoList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    mobile: null,
    name: null,
    avatar: null,
    openId: null,
    gender: null,
    createdAt: null,
    updatedAt: null,
    createdBy: null,
    updatedBy: null,
    sysUserId: null,
    serviceOrderCount: null,
    isContractSigner: null,
    remark: null,
    extend1: null,
    extend2: null
  }
  proxy.resetForm("customerInfoRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加客户信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getCustomerInfo(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改客户信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["customerInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCustomerInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addCustomerInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除客户信息编号为"' + _ids + '"的数据项？').then(function() {
    return delCustomerInfo(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('customer/customerInfo/export', {
    ...queryParams.value
  }, `customerInfo_${new Date().getTime()}.xlsx`)
}

getList()
</script>
